package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.SensitiveDto;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {


    /**
     * 查询所有敏感词
     */
    public ResponseResult list(SensitiveDto dto){
        dto.checkParam();
        QueryWrapper<WmSensitive> wrapper=new QueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like("sensitives", dto.getName());
        }
        IPage page=new Page<WmSensitive>(dto.getPage(),dto.getSize());
        page = page(page, wrapper);

        //3.结果返回
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        System.out.println("分页查询敏感词："+responseResult);
        return responseResult;
    }

    /**
     * 保存敏感词
     * @param wmSensitive
     * @return
     */
    public ResponseResult saveSensitive(WmSensitive wmSensitive){
        if (StringUtils.isBlank(wmSensitive.getSensitives())){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        WmSensitive sensitive=new WmSensitive();
        sensitive.setSensitives(wmSensitive.getSensitives());
        sensitive.setCreatedTime(new Date());

        boolean save = save(sensitive);
        if (!save){
            throw new CustomException(AppHttpCodeEnum.FAIL_TO_SAVE_SENSITIVE);
        }
        return new ResponseResult();
    }

    /**
     * 删除敏感词
     * @param id
     * @return
     */
    public ResponseResult deleteSeneitive(Integer id){
        if (id ==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean b = removeById(id);
        if (!b){
            throw new CustomException(AppHttpCodeEnum.FAIL_TO_DELERE_SENSITIVE);
        }
        return new ResponseResult();
    }

    /**
     * 修改敏感词
     * @param wmSensitive
     * @return
     */
    public ResponseResult updateSemsitive(WmSensitive wmSensitive){
        if (wmSensitive.getId() ==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        if (StringUtils.isBlank(wmSensitive.getSensitives())){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean b = updateById(wmSensitive);
        if (!b){
            throw new CustomException(AppHttpCodeEnum.FAIL_TO_UPDATE_SENSITIVE);
        }
        return new ResponseResult();
    }
}
